/*
 * Wazuh Vulnerability Scanner - Unit Tests
 * Copyright (C) 2015, Wazuh Inc.
 * September 21, 2023.
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License (version 2) as published by the FSF - Free Software
 * Foundation.
 */

#include "scanOrchestrator_test.hpp"
#include "../../../../shared_modules/utils/flatbuffers/include/syscollector_deltas_generated.h"
#include "../../../../shared_modules/utils/flatbuffers/include/syscollector_deltas_schema.h"
#include "../../../../shared_modules/utils/flatbuffers/include/syscollector_synchronization_generated.h"
#include "../../../../shared_modules/utils/flatbuffers/include/syscollector_synchronization_schema.h"
#include "../scanOrchestrator/scanOrchestrator.hpp"
#include "MockFactoryOrchestrator.hpp"
#include "MockSocketDBWrapper.hpp"
#include "TrampolineFactoryOrchestrator.hpp"
#include "TrampolineOsDataCache.hpp"
#include "TrampolineRemediationDataCache.hpp"
#include "TrampolineScanContext.hpp"
#include "TrampolineSocketDBWrapper.hpp"
#include "flatbuffers/flatbuffer_builder.h"
#include "flatbuffers/idl.h"
#include "json.hpp"
#include "osPrimitives.hpp"

auto constexpr TEST_REPORTS_QUEUE_PATH {"queue/vd/reports"};
auto constexpr TEST_REPORTS_BULK_SIZE {1};

using ::testing::_;
using testing::Return;
using ::testing::SetArgReferee;

namespace NSScanOrchestratorTest
{
    const std::string DELTA_PACKAGES_INSERTED_MSG =
        R"(
            {
                "agent_info": {
                    "agent_id": "001",
                    "agent_ip": "192.168.33.20",
                    "agent_name": "focal"
                },
                "data_type": "dbsync_packages",
                "data": {
                    "architecture": "amd64",
                    "checksum": "1e6ce14f97f57d1bbd46ff8e5d3e133171a1bbce",
                    "description": "library for GIF images library",
                    "format": "deb",
                    "groups": "libs",
                    "item_id": "ec465b7eb5fa011a336e95614072e4c7f1a65a53",
                    "multiarch": "same",
                    "name": "libgif7",
                    "priority": "optional",
                    "scan_time": "2023/08/04 19:56:11",
                    "size": 72,
                    "source": "giflib",
                    "vendor": "Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>",
                    "version": "5.1.9-1",
                    "install_time": "1577890801"
                },
                "operation": "INSERTED"
            }
        )";

    const std::string DELTA_PACKAGES_DELETED_MSG =
        R"(
            {
                "agent_info": {
                    "agent_id": "001",
                    "agent_ip": "192.168.33.20",
                    "agent_name": "focal"
                },
                "data_type": "dbsync_packages",
                "data": {
                    "architecture": "amd64",
                    "checksum": "1e6ce14f97f57d1bbd46ff8e5d3e133171a1bbce",
                    "description": "library for GIF images library",
                    "format": "deb",
                    "groups": "libs",
                    "item_id": "ec465b7eb5fa011a336e95614072e4c7f1a65a53",
                    "multiarch": "same",
                    "name": "libgif7",
                    "priority": "optional",
                    "scan_time": "2023/08/04 19:56:11",
                    "size": 72,
                    "source": "giflib",
                    "vendor": "Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>",
                    "version": "5.1.9-1",
                    "install_time": "1577890801"
                },
                "operation": "DELETED"
            }
        )";

    const std::string DELTA_HOTFIXES_INSERTED_MSG =
        R"(
            {
                "agent_info": {
                    "agent_id": "001",
                    "agent_ip": "192.168.33.20",
                    "agent_name": "focal"
                },
                "data_type": "dbsync_hotfixes",
                "data": {
                    "checksum":"56162cd7bb632b4728ec868e8e271b01222ff131",
                    "hotfix":"KB12345678",
                    "scan_time":"2024/01/05 10:30:25"
                },
                "operation": "INSERTED"
            }
        )";

    const std::string DELTA_HOTFIXES_DELETED_MSG =
        R"(
            {
                "agent_info": {
                    "agent_id": "001",
                    "agent_ip": "192.168.33.20",
                    "agent_name": "focal"
                },
                "data_type": "dbsync_hotfixes",
                "data": {
                    "checksum":"56162cd7bb632b4728ec868e8e271b01222ff131",
                    "hotfix":"KB12345678",
                    "scan_time":"2024/01/05 10:30:25"
                },
                "operation": "DELETED"
            }
        )";

    const std::string DELTA_OSINFO_MSG =
        R"(
            {
                "agent_info": {
                    "agent_id": "001",
                    "agent_ip": "192.168.33.20",
                    "agent_name": "focal"
                },
                "data_type": "dbsync_osinfo",
                "data": {
                    "architecture":"x86_64",
                    "checksum":"1691178971959743855",
                    "hostname":"redhat",
                    "os_codename":"9",
                    "os_major":"9",
                    "os_minor":"0",
                    "os_name":"Redhat",
                    "os_patch":"6",
                    "os_platform":"rhel",
                    "os_version":"9.0.6",
                    "release":"5.4.0-155-generic",
                    "scan_time":"2023/08/04 19:56:11",
                    "sysname":"Linux",
                    "version":"#172-Ubuntu SMP Fri Jul 7 16:10:02 UTC 2023"
                },
                "operation": "INSERTED"
            }
        )";

    const std::string SYNCHRONIZATION_INTEGRITY_CLEAR_MSG =
        R"(
            {
                "agent_info": {
                    "agent_id": "001",
                    "agent_ip": "192.168.33.20",
                    "agent_name": "focal"
                },
                "data_type": "integrity_clear",
                "data": {
                    "id": 1700236640,
                    "attributes_type": "syscollector_packages"
                }
            }
        )";

} // namespace NSScanOrchestratorTest

// Shared pointers definitions
std::shared_ptr<MockFactoryOrchestrator> spFactoryOrchestratorMock;
std::shared_ptr<MockSocketDBWrapper> spSocketDBWrapperMock;
std::shared_ptr<TScanContext<TrampolineOsDataCache, GlobalData, TrampolineRemediationDataCache>> spScanContext;

using namespace NSScanOrchestratorTest;

void ScanOrchestratorTest::SetUp() {}

void ScanOrchestratorTest::TearDown()
{
    spFactoryOrchestratorMock.reset();
    spOsDataCacheMock.reset();
    spRemediationDataCacheMock.reset();
    spSocketDBWrapperMock.reset();
    spScanContext.reset();
    Log::deassignLogFunction();
}

TEST_F(ScanOrchestratorTest, TestRunScannerTypePackageInsert)
{
    Os osData {.hostName = "osdata_hostname",
               .architecture = "osdata_architecture",
               .name = "osdata_name",
               .codeName = "osdata_codeName",
               .majorVersion = "osdata_majorVersion",
               .minorVersion = "osdata_minorVersion",
               .patch = "osdata_patch",
               .build = "osdata_build",
               .platform = "osdata_platform",
               .version = "osdata_version",
               .release = "osdata_release",
               .displayVersion = "osdata_displayVersion",
               .sysName = "osdata_sysName",
               .kernelVersion = "osdata_kernelVersion",
               .kernelRelease = "osdata_kernelRelease"};

    spOsDataCacheMock = std::make_shared<MockOsDataCache>();
    EXPECT_CALL(*spOsDataCacheMock, getOsData(_)).WillRepeatedly(testing::Return(osData));

    spRemediationDataCacheMock = std::make_shared<MockRemediationDataCache>();
    EXPECT_CALL(*spRemediationDataCacheMock, getRemediationData(_)).WillRepeatedly(testing::Return(Remediation {}));

    auto spOsOrchestrationMock = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spOsOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageInsertOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageInsertOrchestrationMock, handleRequest(_)).Times(1);

    auto spPackageDeleteOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageDeleteOrchestrationMock, handleRequest(_)).Times(0);

    auto spIntegrityClearOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spIntegrityClearOrchestrationMock, handleRequest(_)).Times(0);

    auto spFetchAllGlobalDb = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spFetchAllGlobalDb, handleRequest(_)).Times(0);

    auto spDeleteAgentScanOrchestration =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spDeleteAgentScanOrchestration, handleRequest(_)).Times(0);

    auto spCleanUpAllOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spCleanUpAllOrchestrationMock, handleRequest(_)).Times(0);

    auto spQueryAllPkgsOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spQueryAllPkgsOrchestrationMock, handleRequest(_)).Times(0);

    spFactoryOrchestratorMock = std::make_shared<MockFactoryOrchestrator>();
    EXPECT_CALL(*spFactoryOrchestratorMock, create())
        .WillOnce(testing::Return(spOsOrchestrationMock))
        .WillOnce(testing::Return(spPackageInsertOrchestrationMock))
        .WillOnce(testing::Return(spPackageDeleteOrchestrationMock))
        .WillOnce(testing::Return(spIntegrityClearOrchestrationMock))
        .WillOnce(testing::Return(spFetchAllGlobalDb))
        .WillOnce(testing::Return(spCleanUpAllOrchestrationMock))
        .WillOnce(testing::Return(spDeleteAgentScanOrchestration))
        .WillOnce(testing::Return(spQueryAllPkgsOrchestrationMock));

    auto spIndexerConnectorMock = std::make_shared<MockIndexerConnector>();

    auto spDatabaseFeedManagerMock = std::make_shared<MockDatabaseFeedManager>();

    flatbuffers::Parser parser;
    ASSERT_TRUE(parser.Parse(syscollector_deltas_SCHEMA));
    ASSERT_TRUE(parser.Parse(DELTA_PACKAGES_INSERTED_MSG.c_str()));
    std::vector<int8_t> bufferVector(parser.builder_.GetBufferPointer(),
                                     parser.builder_.GetBufferPointer() + parser.builder_.GetSize());
    std::variant<const SyscollectorDeltas::Delta*, const SyscollectorSynchronization::SyncMsg*, const nlohmann::json*>
        syscollectorDelta = SyscollectorDeltas::GetDelta(reinterpret_cast<const char*>(bufferVector.data()));

    auto spReportDispatcher = std::make_shared<ReportDispatcher>(
        [](const std::queue<std::string>&)
        {
            // Not used
        },
        TEST_REPORTS_QUEUE_PATH,
        TEST_REPORTS_BULK_SIZE);
    std::shared_mutex mutexScanOrchestrator;

    spScanContext = std::make_shared<TScanContext<TrampolineOsDataCache, GlobalData, TrampolineRemediationDataCache>>(
        syscollectorDelta);

    TScanOrchestrator<TrampolineTScanContext,
                      TrampolineFactoryOrchestrator,
                      MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>,
                      MockIndexerConnector,
                      MockDatabaseFeedManager,
                      OSPrimitives,
                      TrampolineSocketDBWrapper>
        scanOrchestrator(spIndexerConnectorMock, spDatabaseFeedManagerMock, spReportDispatcher, mutexScanOrchestrator);

    flatbuffers::FlatBufferBuilder builder;
    auto object = CreateMessageBufferDirect(
        builder, reinterpret_cast<const std::vector<int8_t>*>(&bufferVector), BufferType::BufferType_DBSync, 0);

    builder.Finish(object);
    auto bufferData = reinterpret_cast<const char*>(builder.GetBufferPointer());
    size_t bufferSize = builder.GetSize();
    const rocksdb::Slice messageSlice(bufferData, bufferSize);

    rocksdb::PinnableSlice pinnableSlice;
    pinnableSlice.PinSlice(messageSlice, nullptr);

    EXPECT_NO_THROW(scanOrchestrator.processEvent(pinnableSlice));
}

TEST_F(ScanOrchestratorTest, TestRunScannerTypePackageDelete)
{
    Os osData {.hostName = "osdata_hostname",
               .architecture = "osdata_architecture",
               .name = "osdata_name",
               .codeName = "osdata_codeName",
               .majorVersion = "osdata_majorVersion",
               .minorVersion = "osdata_minorVersion",
               .patch = "osdata_patch",
               .build = "osdata_build",
               .platform = "osdata_platform",
               .version = "osdata_version",
               .release = "osdata_release",
               .displayVersion = "osdata_displayVersion",
               .sysName = "osdata_sysName",
               .kernelVersion = "osdata_kernelVersion",
               .kernelRelease = "osdata_kernelRelease"};

    spOsDataCacheMock = std::make_shared<MockOsDataCache>();
    EXPECT_CALL(*spOsDataCacheMock, getOsData(_)).WillRepeatedly(testing::Return(osData));

    spRemediationDataCacheMock = std::make_shared<MockRemediationDataCache>();
    EXPECT_CALL(*spRemediationDataCacheMock, getRemediationData(_)).WillRepeatedly(testing::Return(Remediation {}));

    auto spOsOrchestrationMock = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spOsOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageInsertOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageInsertOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageDeleteOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageDeleteOrchestrationMock, handleRequest(_)).Times(1);

    auto spIntegrityClearOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spIntegrityClearOrchestrationMock, handleRequest(_)).Times(0);

    auto spFetchAllGlobalDb = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spFetchAllGlobalDb, handleRequest(_)).Times(0);

    auto spCleanUpAllOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spCleanUpAllOrchestrationMock, handleRequest(_)).Times(0);

    auto spQueryAllPkgsOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spQueryAllPkgsOrchestrationMock, handleRequest(_)).Times(0);

    auto spDeleteAgentScanOrchestration =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spDeleteAgentScanOrchestration, handleRequest(_)).Times(0);

    spFactoryOrchestratorMock = std::make_shared<MockFactoryOrchestrator>();
    EXPECT_CALL(*spFactoryOrchestratorMock, create())
        .WillOnce(testing::Return(spOsOrchestrationMock))
        .WillOnce(testing::Return(spPackageInsertOrchestrationMock))
        .WillOnce(testing::Return(spPackageDeleteOrchestrationMock))
        .WillOnce(testing::Return(spIntegrityClearOrchestrationMock))
        .WillOnce(testing::Return(spFetchAllGlobalDb))
        .WillOnce(testing::Return(spCleanUpAllOrchestrationMock))
        .WillOnce(testing::Return(spDeleteAgentScanOrchestration))
        .WillOnce(testing::Return(spQueryAllPkgsOrchestrationMock));

    auto spIndexerConnectorMock = std::make_shared<MockIndexerConnector>();

    auto spDatabaseFeedManagerMock = std::make_shared<MockDatabaseFeedManager>();

    flatbuffers::Parser parser;
    ASSERT_TRUE(parser.Parse(syscollector_deltas_SCHEMA));
    ASSERT_TRUE(parser.Parse(DELTA_PACKAGES_DELETED_MSG.c_str()));
    std::vector<int8_t> bufferVector(parser.builder_.GetBufferPointer(),
                                     parser.builder_.GetBufferPointer() + parser.builder_.GetSize());
    std::variant<const SyscollectorDeltas::Delta*, const SyscollectorSynchronization::SyncMsg*, const nlohmann::json*>
        syscollectorDelta = SyscollectorDeltas::GetDelta(reinterpret_cast<const char*>(bufferVector.data()));

    auto spReportDispatcher = std::make_shared<ReportDispatcher>(
        [](const std::queue<std::string>&)
        {
            // Not used
        },
        TEST_REPORTS_QUEUE_PATH,
        TEST_REPORTS_BULK_SIZE);
    std::shared_mutex mutexScanOrchestrator;

    spScanContext = std::make_shared<TScanContext<TrampolineOsDataCache, GlobalData, TrampolineRemediationDataCache>>(
        syscollectorDelta);

    TScanOrchestrator<TrampolineTScanContext,
                      TrampolineFactoryOrchestrator,
                      MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>,
                      MockIndexerConnector,
                      MockDatabaseFeedManager,
                      OSPrimitives,
                      TrampolineSocketDBWrapper>
        scanOrchestrator(spIndexerConnectorMock, spDatabaseFeedManagerMock, spReportDispatcher, mutexScanOrchestrator);

    flatbuffers::FlatBufferBuilder builder;
    auto object = CreateMessageBufferDirect(
        builder, reinterpret_cast<const std::vector<int8_t>*>(&bufferVector), BufferType::BufferType_DBSync, 0);

    builder.Finish(object);
    auto bufferData = reinterpret_cast<const char*>(builder.GetBufferPointer());
    size_t bufferSize = builder.GetSize();
    const rocksdb::Slice messageSlice(bufferData, bufferSize);

    rocksdb::PinnableSlice pinnableSlice;
    pinnableSlice.PinSlice(messageSlice, nullptr);

    EXPECT_NO_THROW(scanOrchestrator.processEvent(pinnableSlice));
}

TEST_F(ScanOrchestratorTest, TestRunScannerTypeHotfixInsert)
{
    Os osData {.hostName = "osdata_hostname",
               .architecture = "osdata_architecture",
               .name = "osdata_name",
               .codeName = "osdata_codeName",
               .majorVersion = "osdata_majorVersion",
               .minorVersion = "osdata_minorVersion",
               .patch = "osdata_patch",
               .build = "osdata_build",
               .platform = "osdata_platform",
               .version = "osdata_version",
               .release = "osdata_release",
               .displayVersion = "osdata_displayVersion",
               .sysName = "osdata_sysName",
               .kernelVersion = "osdata_kernelVersion",
               .kernelRelease = "osdata_kernelRelease"};

    spOsDataCacheMock = std::make_shared<MockOsDataCache>();
    EXPECT_CALL(*spOsDataCacheMock, getOsData(_)).WillRepeatedly(testing::Return(osData));

    spRemediationDataCacheMock = std::make_shared<MockRemediationDataCache>();
    EXPECT_CALL(*spRemediationDataCacheMock, getRemediationData(_)).WillRepeatedly(testing::Return(Remediation {}));
    EXPECT_CALL(*spRemediationDataCacheMock, addRemediationData(_, _)).Times(1);

    auto spOsOrchestrationMock = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spOsOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageInsertOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageInsertOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageDeleteOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageDeleteOrchestrationMock, handleRequest(_)).Times(0);

    auto spIntegrityClearOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spIntegrityClearOrchestrationMock, handleRequest(_)).Times(0);

    auto spFetchAllGlobalDb = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spFetchAllGlobalDb, handleRequest(_)).Times(0);

    auto spCleanUpAllOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spCleanUpAllOrchestrationMock, handleRequest(_)).Times(0);

    auto spQueryAllPkgsOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spQueryAllPkgsOrchestrationMock, handleRequest(_)).Times(0);

    auto spDeleteAgentScanOrchestration =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spDeleteAgentScanOrchestration, handleRequest(_)).Times(0);

    spFactoryOrchestratorMock = std::make_shared<MockFactoryOrchestrator>();
    EXPECT_CALL(*spFactoryOrchestratorMock, create())
        .WillOnce(testing::Return(spOsOrchestrationMock))
        .WillOnce(testing::Return(spPackageInsertOrchestrationMock))
        .WillOnce(testing::Return(spPackageDeleteOrchestrationMock))
        .WillOnce(testing::Return(spIntegrityClearOrchestrationMock))
        .WillOnce(testing::Return(spFetchAllGlobalDb))
        .WillOnce(testing::Return(spCleanUpAllOrchestrationMock))
        .WillOnce(testing::Return(spDeleteAgentScanOrchestration))
        .WillOnce(testing::Return(spQueryAllPkgsOrchestrationMock));

    auto spIndexerConnectorMock = std::make_shared<MockIndexerConnector>();

    auto spDatabaseFeedManagerMock = std::make_shared<MockDatabaseFeedManager>();

    flatbuffers::Parser parser;
    ASSERT_TRUE(parser.Parse(syscollector_deltas_SCHEMA));
    ASSERT_TRUE(parser.Parse(DELTA_HOTFIXES_INSERTED_MSG.c_str()));
    std::vector<int8_t> bufferVector(parser.builder_.GetBufferPointer(),
                                     parser.builder_.GetBufferPointer() + parser.builder_.GetSize());
    std::variant<const SyscollectorDeltas::Delta*, const SyscollectorSynchronization::SyncMsg*, const nlohmann::json*>
        syscollectorDelta = SyscollectorDeltas::GetDelta(reinterpret_cast<const char*>(bufferVector.data()));

    auto spReportDispatcher = std::make_shared<ReportDispatcher>(
        [](const std::queue<std::string>&)
        {
            // Not used
        },
        TEST_REPORTS_QUEUE_PATH,
        TEST_REPORTS_BULK_SIZE);
    std::shared_mutex mutexScanOrchestrator;

    spScanContext = std::make_shared<TScanContext<TrampolineOsDataCache, GlobalData, TrampolineRemediationDataCache>>(
        syscollectorDelta);

    TScanOrchestrator<TrampolineTScanContext,
                      TrampolineFactoryOrchestrator,
                      MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>,
                      MockIndexerConnector,
                      MockDatabaseFeedManager,
                      OSPrimitives,
                      TrampolineSocketDBWrapper>
        scanOrchestrator(spIndexerConnectorMock, spDatabaseFeedManagerMock, spReportDispatcher, mutexScanOrchestrator);

    flatbuffers::FlatBufferBuilder builder;
    auto object = CreateMessageBufferDirect(
        builder, reinterpret_cast<const std::vector<int8_t>*>(&bufferVector), BufferType::BufferType_DBSync, 0);

    builder.Finish(object);
    auto bufferData = reinterpret_cast<const char*>(builder.GetBufferPointer());
    size_t bufferSize = builder.GetSize();
    const rocksdb::Slice messageSlice(bufferData, bufferSize);

    rocksdb::PinnableSlice pinnableSlice;
    pinnableSlice.PinSlice(messageSlice, nullptr);

    EXPECT_NO_THROW(scanOrchestrator.processEvent(pinnableSlice));
}

TEST_F(ScanOrchestratorTest, TestRunScannerTypeHotfixDelete)
{
    Os osData {.hostName = "osdata_hostname",
               .architecture = "osdata_architecture",
               .name = "osdata_name",
               .codeName = "osdata_codeName",
               .majorVersion = "osdata_majorVersion",
               .minorVersion = "osdata_minorVersion",
               .patch = "osdata_patch",
               .build = "osdata_build",
               .platform = "osdata_platform",
               .version = "osdata_version",
               .release = "osdata_release",
               .displayVersion = "osdata_displayVersion",
               .sysName = "osdata_sysName",
               .kernelVersion = "osdata_kernelVersion",
               .kernelRelease = "osdata_kernelRelease"};

    spOsDataCacheMock = std::make_shared<MockOsDataCache>();
    EXPECT_CALL(*spOsDataCacheMock, getOsData(_)).WillRepeatedly(testing::Return(osData));

    spRemediationDataCacheMock = std::make_shared<MockRemediationDataCache>();
    EXPECT_CALL(*spRemediationDataCacheMock, getRemediationData(_)).WillRepeatedly(testing::Return(Remediation {}));

    auto spOsOrchestrationMock = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spOsOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageInsertOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageInsertOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageDeleteOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageDeleteOrchestrationMock, handleRequest(_)).Times(0);

    auto spIntegrityClearOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spIntegrityClearOrchestrationMock, handleRequest(_)).Times(0);

    auto spFetchAllGlobalDb = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spFetchAllGlobalDb, handleRequest(_)).Times(0);

    auto spCleanUpAllOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spCleanUpAllOrchestrationMock, handleRequest(_)).Times(0);

    auto spQueryAllPkgsOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spQueryAllPkgsOrchestrationMock, handleRequest(_)).Times(0);

    auto spDeleteAgentScanOrchestration =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spDeleteAgentScanOrchestration, handleRequest(_)).Times(0);

    spFactoryOrchestratorMock = std::make_shared<MockFactoryOrchestrator>();
    EXPECT_CALL(*spFactoryOrchestratorMock, create())
        .WillOnce(testing::Return(spOsOrchestrationMock))
        .WillOnce(testing::Return(spPackageInsertOrchestrationMock))
        .WillOnce(testing::Return(spPackageDeleteOrchestrationMock))
        .WillOnce(testing::Return(spIntegrityClearOrchestrationMock))
        .WillOnce(testing::Return(spFetchAllGlobalDb))
        .WillOnce(testing::Return(spCleanUpAllOrchestrationMock))
        .WillOnce(testing::Return(spDeleteAgentScanOrchestration))
        .WillOnce(testing::Return(spQueryAllPkgsOrchestrationMock));

    auto spIndexerConnectorMock = std::make_shared<MockIndexerConnector>();

    auto spDatabaseFeedManagerMock = std::make_shared<MockDatabaseFeedManager>();

    flatbuffers::Parser parser;
    ASSERT_TRUE(parser.Parse(syscollector_deltas_SCHEMA));
    ASSERT_TRUE(parser.Parse(DELTA_HOTFIXES_DELETED_MSG.c_str()));
    std::vector<int8_t> bufferVector(parser.builder_.GetBufferPointer(),
                                     parser.builder_.GetBufferPointer() + parser.builder_.GetSize());
    std::variant<const SyscollectorDeltas::Delta*, const SyscollectorSynchronization::SyncMsg*, const nlohmann::json*>
        syscollectorDelta = SyscollectorDeltas::GetDelta(reinterpret_cast<const char*>(bufferVector.data()));

    auto spReportDispatcher = std::make_shared<ReportDispatcher>(
        [](const std::queue<std::string>&)
        {
            // Not used
        },
        TEST_REPORTS_QUEUE_PATH,
        TEST_REPORTS_BULK_SIZE);
    std::shared_mutex mutexScanOrchestrator;

    spScanContext = std::make_shared<TScanContext<TrampolineOsDataCache, GlobalData, TrampolineRemediationDataCache>>(
        syscollectorDelta);

    TScanOrchestrator<TrampolineTScanContext,
                      TrampolineFactoryOrchestrator,
                      MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>,
                      MockIndexerConnector,
                      MockDatabaseFeedManager,
                      OSPrimitives,
                      TrampolineSocketDBWrapper>
        scanOrchestrator(spIndexerConnectorMock, spDatabaseFeedManagerMock, spReportDispatcher, mutexScanOrchestrator);

    flatbuffers::FlatBufferBuilder builder;
    auto object = CreateMessageBufferDirect(
        builder, reinterpret_cast<const std::vector<int8_t>*>(&bufferVector), BufferType::BufferType_DBSync, 0);

    builder.Finish(object);
    auto bufferData = reinterpret_cast<const char*>(builder.GetBufferPointer());
    size_t bufferSize = builder.GetSize();
    const rocksdb::Slice messageSlice(bufferData, bufferSize);

    rocksdb::PinnableSlice pinnableSlice;
    pinnableSlice.PinSlice(messageSlice, nullptr);

    EXPECT_NO_THROW(scanOrchestrator.processEvent(pinnableSlice));
}

TEST_F(ScanOrchestratorTest, TestRunScannerTypeOs)
{
    spOsDataCacheMock = std::make_shared<MockOsDataCache>();
    EXPECT_CALL(*spOsDataCacheMock, setOsData(_, _)).Times(1);

    auto spOsOrchestrationMock = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spOsOrchestrationMock, handleRequest(_)).Times(1);

    auto spPackageInsertOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageInsertOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageDeleteOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageDeleteOrchestrationMock, handleRequest(_)).Times(0);

    auto spIntegrityClearOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spIntegrityClearOrchestrationMock, handleRequest(_)).Times(0);

    auto spFetchAllGlobalDb = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spFetchAllGlobalDb, handleRequest(_)).Times(0);

    auto spCleanUpAllOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spCleanUpAllOrchestrationMock, handleRequest(_)).Times(0);

    auto spQueryAllPkgsOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spQueryAllPkgsOrchestrationMock, handleRequest(_)).Times(0);

    auto spDeleteAgentScanOrchestration =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spDeleteAgentScanOrchestration, handleRequest(_)).Times(0);

    spFactoryOrchestratorMock = std::make_shared<MockFactoryOrchestrator>();
    EXPECT_CALL(*spFactoryOrchestratorMock, create())
        .WillOnce(testing::Return(spOsOrchestrationMock))
        .WillOnce(testing::Return(spPackageInsertOrchestrationMock))
        .WillOnce(testing::Return(spPackageDeleteOrchestrationMock))
        .WillOnce(testing::Return(spIntegrityClearOrchestrationMock))
        .WillOnce(testing::Return(spFetchAllGlobalDb))
        .WillOnce(testing::Return(spCleanUpAllOrchestrationMock))
        .WillOnce(testing::Return(spDeleteAgentScanOrchestration))
        .WillOnce(testing::Return(spQueryAllPkgsOrchestrationMock));

    auto spIndexerConnectorMock = std::make_shared<MockIndexerConnector>();

    auto spDatabaseFeedManagerMock = std::make_shared<MockDatabaseFeedManager>();

    flatbuffers::Parser parser;
    ASSERT_TRUE(parser.Parse(syscollector_deltas_SCHEMA));
    ASSERT_TRUE(parser.Parse(DELTA_OSINFO_MSG.c_str()));
    std::vector<int8_t> bufferVector(parser.builder_.GetBufferPointer(),
                                     parser.builder_.GetBufferPointer() + parser.builder_.GetSize());

    std::variant<const SyscollectorDeltas::Delta*, const SyscollectorSynchronization::SyncMsg*, const nlohmann::json*>
        syscollectorDelta = SyscollectorDeltas::GetDelta(reinterpret_cast<const char*>(bufferVector.data()));

    auto spReportDispatcher = std::make_shared<ReportDispatcher>(
        [](const std::queue<std::string>&)
        {
            // Not used
        },
        TEST_REPORTS_QUEUE_PATH,
        TEST_REPORTS_BULK_SIZE);
    std::shared_mutex mutexScanOrchestrator;

    spScanContext = std::make_shared<TScanContext<TrampolineOsDataCache, GlobalData, TrampolineRemediationDataCache>>(
        syscollectorDelta);

    TScanOrchestrator<TrampolineTScanContext,
                      TrampolineFactoryOrchestrator,
                      MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>,
                      MockIndexerConnector,
                      MockDatabaseFeedManager,
                      OSPrimitives,
                      TrampolineSocketDBWrapper>

        scanOrchestrator(spIndexerConnectorMock, spDatabaseFeedManagerMock, spReportDispatcher, mutexScanOrchestrator);

    flatbuffers::FlatBufferBuilder builder;
    auto object = CreateMessageBufferDirect(
        builder, reinterpret_cast<const std::vector<int8_t>*>(&bufferVector), BufferType::BufferType_DBSync, 0);

    builder.Finish(object);
    auto bufferData = reinterpret_cast<const char*>(builder.GetBufferPointer());
    size_t bufferSize = builder.GetSize();
    const rocksdb::Slice messageSlice(bufferData, bufferSize);

    rocksdb::PinnableSlice pinnableSlice;
    pinnableSlice.PinSlice(messageSlice, nullptr);

    EXPECT_NO_THROW(scanOrchestrator.processEvent(pinnableSlice));
}

TEST_F(ScanOrchestratorTest, TestRunScannerTypeIntegrityClear)
{
    Os osData {.hostName = "osdata_hostname",
               .architecture = "osdata_architecture",
               .name = "osdata_name",
               .codeName = "osdata_codeName",
               .majorVersion = "osdata_majorVersion",
               .minorVersion = "osdata_minorVersion",
               .patch = "osdata_patch",
               .build = "osdata_build",
               .platform = "osdata_platform",
               .version = "osdata_version",
               .release = "osdata_release",
               .displayVersion = "osdata_displayVersion",
               .sysName = "osdata_sysName",
               .kernelVersion = "osdata_kernelVersion",
               .kernelRelease = "osdata_kernelRelease"};

    spOsDataCacheMock = std::make_shared<MockOsDataCache>();
    EXPECT_CALL(*spOsDataCacheMock, getOsData(_)).WillRepeatedly(testing::Return(osData));

    spRemediationDataCacheMock = std::make_shared<MockRemediationDataCache>();
    EXPECT_CALL(*spRemediationDataCacheMock, getRemediationData(_)).WillRepeatedly(testing::Return(Remediation {}));

    auto spOsOrchestrationMock = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spOsOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageInsertOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageInsertOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageDeleteOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageDeleteOrchestrationMock, handleRequest(_)).Times(0);

    auto spIntegrityClearOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spIntegrityClearOrchestrationMock, handleRequest(_)).Times(1);

    auto spFetchAllGlobalDb = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spFetchAllGlobalDb, handleRequest(_)).Times(0);

    auto spCleanUpAllOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spCleanUpAllOrchestrationMock, handleRequest(_)).Times(0);

    auto spQueryAllPkgsOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spQueryAllPkgsOrchestrationMock, handleRequest(_)).Times(0);

    auto spDeleteAgentScanOrchestration =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spDeleteAgentScanOrchestration, handleRequest(_)).Times(0);

    spFactoryOrchestratorMock = std::make_shared<MockFactoryOrchestrator>();
    EXPECT_CALL(*spFactoryOrchestratorMock, create())
        .WillOnce(testing::Return(spOsOrchestrationMock))
        .WillOnce(testing::Return(spPackageInsertOrchestrationMock))
        .WillOnce(testing::Return(spPackageDeleteOrchestrationMock))
        .WillOnce(testing::Return(spIntegrityClearOrchestrationMock))
        .WillOnce(testing::Return(spFetchAllGlobalDb))
        .WillOnce(testing::Return(spCleanUpAllOrchestrationMock))
        .WillOnce(testing::Return(spDeleteAgentScanOrchestration))
        .WillOnce(testing::Return(spQueryAllPkgsOrchestrationMock));

    auto spIndexerConnectorMock = std::make_shared<MockIndexerConnector>();

    auto spDatabaseFeedManagerMock = std::make_shared<MockDatabaseFeedManager>();

    flatbuffers::Parser parser;
    ASSERT_TRUE(parser.Parse(syscollector_synchronization_SCHEMA));
    ASSERT_TRUE(parser.Parse(SYNCHRONIZATION_INTEGRITY_CLEAR_MSG.c_str()));
    std::vector<int8_t> bufferVector(parser.builder_.GetBufferPointer(),
                                     parser.builder_.GetBufferPointer() + parser.builder_.GetSize());

    std::variant<const SyscollectorDeltas::Delta*, const SyscollectorSynchronization::SyncMsg*, const nlohmann::json*>
        syscollectorSynchronization =
            SyscollectorSynchronization::GetSyncMsg(reinterpret_cast<const char*>(bufferVector.data()));

    auto spReportDispatcher = std::make_shared<ReportDispatcher>(
        [](const std::queue<std::string>&)
        {
            // Not used
        },
        TEST_REPORTS_QUEUE_PATH,
        TEST_REPORTS_BULK_SIZE);
    std::shared_mutex mutexScanOrchestrator;

    spScanContext = std::make_shared<TScanContext<TrampolineOsDataCache, GlobalData, TrampolineRemediationDataCache>>(
        syscollectorSynchronization);

    TScanOrchestrator<TrampolineTScanContext,
                      TrampolineFactoryOrchestrator,
                      MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>,
                      MockIndexerConnector,
                      MockDatabaseFeedManager,
                      OSPrimitives,
                      TrampolineSocketDBWrapper>

        scanOrchestrator(spIndexerConnectorMock, spDatabaseFeedManagerMock, spReportDispatcher, mutexScanOrchestrator);

    flatbuffers::FlatBufferBuilder builder;
    auto object = CreateMessageBufferDirect(
        builder, reinterpret_cast<const std::vector<int8_t>*>(&bufferVector), BufferType::BufferType_RSync, 0);

    builder.Finish(object);
    auto bufferData = reinterpret_cast<const char*>(builder.GetBufferPointer());
    size_t bufferSize = builder.GetSize();
    const rocksdb::Slice messageSlice(bufferData, bufferSize);

    rocksdb::PinnableSlice pinnableSlice;
    pinnableSlice.PinSlice(messageSlice, nullptr);

    EXPECT_NO_THROW(scanOrchestrator.processEvent(pinnableSlice));
}

TEST_F(ScanOrchestratorTest, TestRunScannerTypePackageInsertInDelayed)
{
    Os osData {.hostName = "osdata_hostname",
               .architecture = "osdata_architecture",
               .name = "osdata_name",
               .codeName = "osdata_codeName",
               .majorVersion = "osdata_majorVersion",
               .minorVersion = "osdata_minorVersion",
               .patch = "osdata_patch",
               .build = "osdata_build",
               .platform = "osdata_platform",
               .version = "osdata_version",
               .release = "osdata_release",
               .displayVersion = "osdata_displayVersion",
               .sysName = "osdata_sysName",
               .kernelVersion = "osdata_kernelVersion",
               .kernelRelease = "osdata_kernelRelease"};

    spOsDataCacheMock = std::make_shared<MockOsDataCache>();
    EXPECT_CALL(*spOsDataCacheMock, getOsData(_)).WillRepeatedly(testing::Return(osData));

    spRemediationDataCacheMock = std::make_shared<MockRemediationDataCache>();
    EXPECT_CALL(*spRemediationDataCacheMock, getRemediationData(_)).WillRepeatedly(testing::Return(Remediation {}));

    auto spOsOrchestrationMock = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spOsOrchestrationMock, handleRequest(_)).Times(0);

    auto spPackageInsertOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageInsertOrchestrationMock, handleRequest(_)).Times(2);

    auto spPackageDeleteOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spPackageDeleteOrchestrationMock, handleRequest(_)).Times(0);

    auto spIntegrityClearOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spIntegrityClearOrchestrationMock, handleRequest(_)).Times(0);

    auto spFetchAllGlobalDb = std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spFetchAllGlobalDb, handleRequest(_)).Times(0);

    auto spDeleteAgentScanOrchestration =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spDeleteAgentScanOrchestration, handleRequest(_)).Times(0);

    auto spCleanUpAllOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spCleanUpAllOrchestrationMock, handleRequest(_)).Times(0);

    auto spQueryAllPkgsOrchestrationMock =
        std::make_shared<MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>>();
    EXPECT_CALL(*spQueryAllPkgsOrchestrationMock, handleRequest(_)).Times(0);

    spFactoryOrchestratorMock = std::make_shared<MockFactoryOrchestrator>();
    EXPECT_CALL(*spFactoryOrchestratorMock, create())
        .WillOnce(testing::Return(spOsOrchestrationMock))
        .WillOnce(testing::Return(spPackageInsertOrchestrationMock))
        .WillOnce(testing::Return(spPackageDeleteOrchestrationMock))
        .WillOnce(testing::Return(spIntegrityClearOrchestrationMock))
        .WillOnce(testing::Return(spFetchAllGlobalDb))
        .WillOnce(testing::Return(spCleanUpAllOrchestrationMock))
        .WillOnce(testing::Return(spDeleteAgentScanOrchestration))
        .WillOnce(testing::Return(spQueryAllPkgsOrchestrationMock));

    auto spIndexerConnectorMock = std::make_shared<MockIndexerConnector>();

    auto spDatabaseFeedManagerMock = std::make_shared<MockDatabaseFeedManager>();

    flatbuffers::Parser parser;
    ASSERT_TRUE(parser.Parse(syscollector_deltas_SCHEMA));
    ASSERT_TRUE(parser.Parse(DELTA_PACKAGES_INSERTED_MSG.c_str()));
    std::vector<int8_t> bufferVector(parser.builder_.GetBufferPointer(),
                                     parser.builder_.GetBufferPointer() + parser.builder_.GetSize());
    std::variant<const SyscollectorDeltas::Delta*, const SyscollectorSynchronization::SyncMsg*, const nlohmann::json*>
        syscollectorDelta = SyscollectorDeltas::GetDelta(reinterpret_cast<const char*>(bufferVector.data()));

    auto spReportDispatcher = std::make_shared<ReportDispatcher>(
        [](const std::queue<std::string>&)
        {
            // Not used
        },
        TEST_REPORTS_QUEUE_PATH,
        TEST_REPORTS_BULK_SIZE);
    std::shared_mutex mutexScanOrchestrator;

    spScanContext = std::make_shared<TScanContext<TrampolineOsDataCache, GlobalData, TrampolineRemediationDataCache>>(
        syscollectorDelta);

    TScanOrchestrator<TrampolineTScanContext,
                      TrampolineFactoryOrchestrator,
                      MockAbstractHandler<std::shared_ptr<TrampolineTScanContext>>,
                      MockIndexerConnector,
                      MockDatabaseFeedManager,
                      OSPrimitives,
                      TrampolineSocketDBWrapper,
                      1> // Delayed time 1 second
        scanOrchestrator(spIndexerConnectorMock, spDatabaseFeedManagerMock, spReportDispatcher, mutexScanOrchestrator);

    flatbuffers::FlatBufferBuilder builder;
    auto object = CreateMessageBufferDirect(
        builder, reinterpret_cast<const std::vector<int8_t>*>(&bufferVector), BufferType::BufferType_DBSync, 0);

    builder.Finish(object);
    auto bufferData = reinterpret_cast<const char*>(builder.GetBufferPointer());
    size_t bufferSize = builder.GetSize();
    const rocksdb::Slice messageSlice(bufferData, bufferSize);

    rocksdb::PinnableSlice pinnableSlice;
    pinnableSlice.PinSlice(messageSlice, nullptr);

    EXPECT_NO_THROW(scanOrchestrator.pushEventToDelayedDispatcher(pinnableSlice, "001"));
    EXPECT_NO_THROW(scanOrchestrator.processEvent(pinnableSlice));
    // The sleep is necessary to wait for the delayed event to be processed
    std::this_thread::sleep_for(std::chrono::seconds(10));
}
